#include "jniHelper.h"
#include "jniGLBufferView.h"
#include "jniSurfaceView.h"
#include "jniBitmapView.h"

#ifndef NELEM
#define NELEM(x) ((int)(sizeof(x) / sizeof((x)[0])))
#endif

static int register_native_methods(JNIEnv *env,
                                   const char *class_name,
                                   JNINativeMethod *methods,
                                   int num_methods);

static const char *glbufferview_class_path_name = "com/oe/vdi/ui/JniGLBufferView";
static JNINativeMethod glbufferview_methods[] = {
        {"native_start",     "()V",   (void *) native_start},
        {"native_gl_resize", "(II)V", (void *) native_gl_resize},
        {"native_gl_render", "()V",   (void *) native_gl_render},
};

static const char *jnisurfaceview_class_path_name = "com/oe/vdi/ui/JniSurfaceView";
static JNINativeMethod jnisurfaceview_methods[] = {
        {"attachSurface", "(Landroid/view/Surface;)V", (void *) JniSurfaceView_attachSurface},
        {"detachSurface", "()V",                       (void *) JniSurfaceView_detachSurface},
        {"reDraw",        "()V",                       (void *) JniSurfaceView_reDraw},
};

static const char *jnibitmapview_class_path_name = "com/oe/vdi/ui/JniBitmapView";
static JNINativeMethod jnibitmapview_methods[] = {
        {"updateGraphics", "(Landroid/graphics/Bitmap;)V", (void *) JniBitmapView_updateGraphics},
};

int register_natives(JNIEnv *env) {
    int ret = register_native_methods(env,
                                      glbufferview_class_path_name,
                                      glbufferview_methods,
                                      NELEM(glbufferview_methods));
    if (!ret) return JNI_FALSE;
    ret = register_native_methods(env,
                                  jnisurfaceview_class_path_name,
                                  jnisurfaceview_methods,
                                  NELEM(jnisurfaceview_methods));
    if (!ret) return JNI_FALSE;

    ret = register_native_methods(env,
                                  jnibitmapview_class_path_name,
                                  jnibitmapview_methods,
                                  NELEM(jnibitmapview_methods));
    if (!ret) return JNI_FALSE;

    return JNI_TRUE;
}

static int register_native_methods(JNIEnv *env,
                                   const char *class_name,
                                   JNINativeMethod *methods,
                                   int num_methods) {
    jclass clazz;

    clazz = (*env)->FindClass(env, class_name);
    if (clazz == NULL) {
        LOGE("Native registration unable to find class '%s'\n",
                class_name);
        return JNI_FALSE;
    }
    if ((*env)->RegisterNatives(env, clazz, methods, num_methods) < 0) {
        LOGE("RegisterNatives failed for '%s'\n", class_name);
        return JNI_FALSE;
    }

    return JNI_TRUE;
}



